package cn.stylefeng.guns.modular.fileTag.service;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.modular.fileTag.mapper.FileTagMapper;
import cn.stylefeng.guns.modular.fileTag.model.FileTagParam;
import cn.stylefeng.guns.modular.fileTag.model.FileTagResult;
import cn.stylefeng.guns.modular.leaveinfo.service.LeaveInfoService;
import cn.stylefeng.guns.modular.studentmanage.service.StuManageService;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FileTagService extends ServiceImpl<FileTagMapper, FileTagPO> {

    @Autowired
    private FileTagMapper fileTagMapper;

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    @Autowired
    private StuManageService stuManageService;

    @Autowired
    private LeaveInfoService leaveInfoService;

    //调整数据
    public List<FileTagResult> processList(Page page, FileTagParam fileTagParam) {
        List<FileTagPO> fileTagPOS = fileTagMapper.list(page, fileTagParam.getCondition());
        List<FileTagResult> results = new ArrayList<>(fileTagPOS.size());
        if (!CollectionUtils.isEmpty(fileTagPOS)) {
            fileTagPOS.forEach(fileTagPO -> {
                FileTagResult fileTagResult = new FileTagResult();
                BeanUtils.copyProperties(fileTagPO, fileTagResult);
                fileTagResult.setShowCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fileTagPO.getCreateTime() * 1000));
                if (null != fileTagPO.getUpdateTime()) {
                    fileTagResult.setShowUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fileTagPO.getUpdateTime() * 1000));
                }
                fileTagResult.setTypeText(fileTagPO.getType() + "");
                Integer totalPeoples = calculationAllPeoples(fileTagPO.getId());
                Integer leavePeoples = calculationLeavePeoples(fileTagPO.getId());
                //总人数
                fileTagResult.setTotalPeoples(totalPeoples);
                //在位人数
                Integer incumbentsPeoples = totalPeoples- leavePeoples;
                fileTagResult.setIncumbentsPeoples(incumbentsPeoples);
                //请假人数==总人数-在位人数
                fileTagResult.setLeavePeoples(leavePeoples);
                results.add(fileTagResult);
            });
        }
        return results;
    }

    public void consummateInfo(FileTagPO fileTagPO) {
        //根据当前用户获取其所属部门Id及其所属部门名称
        Long sonId = LoginContextHolder.getContext().getUser().getDeptId();
        String sonName = LoginContextHolder.getContext().getUser().getDeptName();
        Dept sonDept = deptService.getById(sonId);
        //获取其父级部门及名称
        Long dadId = sonDept.getPid();
        Dept dadDept = deptService.getById(dadId);
        String dadFullName = dadDept.getFullName();
        //获取其爷级部门id级名称
        Long grandpaId = dadDept.getPid();
        Dept grandpaDept = deptService.getById(grandpaId);
        String grandpaName = grandpaDept.getFullName();
        //获取父亲对应的级别的所属的管理员
        User dadUser = userService.getOne(new QueryWrapper<User>().eq("dept_id", dadId));
        fileTagPO.setFoxbaseApprovalId(dadUser.getUserId());
        //获取爷爷对应的级别的所属管理员
        User grandpaIdUser = userService.getOne(new QueryWrapper<User>().eq("dept_id", grandpaId));
        fileTagPO.setTertiaryApprovalId(grandpaIdUser.getUserId());
    }

    //计算本班所有人
    public Integer calculationAllPeoples(Long id) {
        //查询这个班级下得所有学生
        List<LeaveUserPO> LeaveUserPOs = stuManageService.list(new QueryWrapper<LeaveUserPO>().eq("clbum_id", id));
        if (!CollectionUtils.isEmpty(LeaveUserPOs)) {
            return LeaveUserPOs.size();
        }
        return 0;
    }

    //计算本班请假人数
    public Integer calculationLeavePeoples(Long id) {
        //查出本班所有学生
        List<LeaveUserPO> leaveUserPOs = stuManageService.list(new QueryWrapper<LeaveUserPO>().eq("clbum_id", id));
        if (!CollectionUtils.isEmpty(leaveUserPOs)) {
            //获取所有学生记录得主键id，注意这里是主键不是学生学号
            List<String> ids = leaveUserPOs.stream().map(LeaveUserPO::getLeaveUserNum).collect(Collectors.toList());
            //根据学号获取所有得请假信息
            List<LeaveInfoPO> leaveInfoPOS = leaveInfoService.list(new QueryWrapper<LeaveInfoPO>().in("leave_user_num",ids));
            //过滤请假状态为1--即已审批，则定义为不在位
            List<LeaveInfoPO> leavePeoples = leaveInfoPOS.stream().filter(leaveInfoPO -> leaveInfoPO.getLeaveInfoStatus() == 1).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(leavePeoples)) {
                return leavePeoples.size();
            }
        }
        return 0;
    }
}
